//
//  WHYStatusViewModel.swift
//  WHYWeibo-1.0
//
//  Created by 王洪运 on 15/11/25.
//  Copyright © 2015年 王洪运. All rights reserved.
//

import UIKit

class WHYStatusViewModel {

    /// 微博消息模型
    var status: WHYStatus
    /// 发布日期
    var createdTime: String? {
        return NSDate.sinaDate(status.created_at ?? "")?.dateDescription
    }
    /// 配图 URL 数组
    var thumbnailUrls: [NSURL]?
    /// 被转发原创微博的文字
    var retweetedText: String? {
        guard let s = status.retweeted_status else {
            return nil
        }
        return "@" + (s.user?.screen_name ?? "") + ":" + (s.text ?? "")
    }
    /// cell的可重用ID
    var cellID: String {
        return status.retweeted_status == nil ? WHYStatusCellNormalID : WHYStatusCellRetweetedID
    }
    /// 缓存的行高
    lazy var rowHeight: CGFloat = {
        var cell: WHYStatusCell
        
        if self.status.retweeted_status != nil {
            cell = WHYStatusRetweetedCell(style: .Default, reuseIdentifier: WHYStatusCellRetweetedID)
        } else {
            cell = WHYStatusNormalCell(style: .Default, reuseIdentifier: WHYStatusCellNormalID)
        }
        
        return cell.rowHeight(self)
    }()
    
    /// 用户头像地址
    var userProfileURL: NSURL {
        return NSURL(string: status.user?.profile_image_url ?? "")!
    }
    /// 用户默认头像
    var userDefaultImage: UIImage {
        return UIImage(named: "avatar_default_big")!
    }
    /// VIP图标
    var userVipImage: UIImage? {
        
        switch status.user?.verified_type ?? -1 {
        case 0: return UIImage(named: "avatar_vip")
        case 2, 3, 4: return UIImage(named: "avatar_enterprise_vip")
        case 220: return UIImage(named: "avatar_grassroot")
        default : return nil
        }
        
    }
    /// 认证图标
    var userMemberImage: UIImage? {
        
        if status.user?.mbrank > 0 && status.user?.mbrank < 7 {
            return UIImage(named: "common_icon_membership_level\(status.user?.mbrank)")
        }
        return nil
    }

    
    init(status: WHYStatus) {
        self.status = status
        
        let urls = status.retweeted_status?.pic_urls ?? status.pic_urls
        if urls?.count > 0{
        
            thumbnailUrls = [NSURL]()
            
            for dict in urls! {
                let url = NSURL(string: dict["thumbnail_pic"]!)
                thumbnailUrls?.append(url!)
            }
            
        }
        
    }
    
}

// MARK: - CustomStringConvertible
extension WHYStatusViewModel: CustomStringConvertible {
    
    var description: String {
        return status.description + "\n配图数组\((thumbnailUrls ?? []) as Array)"
    }
    
}





